package com.boshanam.service.user.serviceimpl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UsernameNotFoundException;

import com.boshanam.exceptions.domain.user.EmailIdAlreadyRegisteredException;
import com.boshanam.exceptions.domain.user.InvalidUserException;
import com.boshanam.exceptions.domain.user.UserAlreadyExistException;
import com.boshanam.exceptions.domain.user.UserNameAlreadyExistsException;
import com.boshanam.exceptions.domain.user.UserNotExistException;
import com.boshanam.model.dao.IUserDao;
import com.boshanam.model.vos.domain.user.GrantedAuthorityImplVO;
import com.boshanam.model.vos.domain.user.UserVO;
import com.boshanam.model.vos.domain.user.UserVOImmutable;
import com.boshanam.service.user.IUserService;
import com.boshanam.util.user.UserUtils;

/**
 * @author Sivakumar Y, UserServiceGoogleDsImpl.java Dec 24, 2009 11:19:15 AM
 *         www.ibigbang.net shiva.forums@gmail.com
 */
public class UserServiceImpl implements IUserService {
	
	Log log = LogFactory.getLog(UserServiceImpl.class);

	private IUserDao iUserDao;

	public UserServiceImpl() {
	}

	public void setUserDao(IUserDao iUserDao) {
		this.iUserDao = iUserDao;
	}

	public IUserDao getUserDao() {
		return iUserDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.ibigbang.service.UserService#createUser(net.ibigbang.model.vos.users
	 * .UserVO)
	 */
	@Override
	public void createUser(UserVO user) throws InvalidUserException,
			DataAccessException, UserNameAlreadyExistsException,
			EmailIdAlreadyRegisteredException, UserAlreadyExistException {

		try {
			if(user.getUsername() == null){
				user.setUsername(user.getEmail().substring(0, user.getEmail().indexOf('@')));
			}
			if(user.getAuthorities() == null || user.getAuthorities().length < 1){
				user.setAuthorities(new GrantedAuthority[]{new GrantedAuthorityImplVO("ROLE_USER")});
			}
			log.fatal("USER_SERVICE-The Locale associated with user: " + user.getUserSettings().getLocale());
			this.getUserDao().save(UserUtils.createEntityFromVO(user));
		} catch (UserNameAlreadyExistsException e) {
			log.debug("UserName Already Exists", e);
			throw e;
		} catch (EmailIdAlreadyRegisteredException e) {
			log.debug("Email Id Already Exists", e);
			throw e;
		} catch (UserAlreadyExistException e) {
			log.debug("User already Registered...with username and EmailId", e);
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.ibigbang.service.UserService#deleteUser(net.ibigbang.model.vos.users
	 * .UserVO)
	 */
	@Override
	public void deleteUser(UserVO user) throws InvalidUserException,
			UserNotExistException, DataAccessException {
		getUserDao().delete(UserUtils.createEntityFromVO(user));

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.ibigbang.service.UserService#deleteUserByEmail(java.lang.String)
	 */
	@Override
	public void deleteUserByEmail(String emailId) throws InvalidUserException,
			UserNotExistException, DataAccessException {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.ibigbang.service.UserService#deleteUserByUserId(net.ibigbang.model
	 * .vos.users.UserVO)
	 */
	@Override
	public void deleteUserByUserId(UserVO userId) throws InvalidUserException,
			UserNotExistException, DataAccessException {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.ibigbang.service.UserService#loadUserByEmail(java.lang.String)
	 */
	@Override
	public UserVOImmutable loadUserByEmail(String emailId)
			throws UserNotExistException, DataAccessException {
		return getUserDao().getUserByEmail(emailId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.ibigbang.service.UserService#updateUser(net.ibigbang.model.vos.users
	 * .UserVO)
	 */
	@Override
	public UserVO updateUser(UserVO user) throws InvalidUserException,
			DataAccessException {
		return getUserDao().updateUser(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.acegisecurity.userdetails.UserDetailsService#loadUserByUsername(java
	 * .lang.String)
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		if(username.indexOf("@") > 0){
			return getUserDao().getUserByEmail(username);
		}
		return getUserDao().loadUserByUsername(username);

	}

	/* (non-Javadoc)
	 * @see net.ibigbang.service.UserService#getAllUsers(int)
	 */
	@Override
	public List<UserVOImmutable> getAllUsers(int max)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see net.ibigbang.service.UserService#getAllUsers()
	 */
	@Override
	public List<UserVOImmutable> getAllUsers() throws DataAccessException {
		
		return getUserDao().getAllUsers();
	}

}
